<template>
  <!-- separator分隔符 -->
  <el-cascader :value="value" separator="-" size="mini" :props="props" :options="depts" @change="changeValue" />
</template>
<script>
import { getDepartmentList } from '@/api/department'
import { translateListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depts: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取部门列表
    async getDepartmentList() {
      const res = await getDepartmentList()
      // console.log(res)
      this.depts = translateListToTreeData(res, 0)
    },
    changeValue(list) {
      if (list.length) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
      console.log(list)
    }
  }
}
</script>
